package com.haipumi.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.haipumi.beans.haipumi_admins;
import com.haipumi.dao.DBHelper;
import com.haipumi.dao.IBackAdminDao;
import com.haipumi.utils.Encrypt;

public class BackAdminDaoImpl implements IBackAdminDao {
	
	DBHelper db=new  DBHelper();
	
	
	
	@Override
	public haipumi_admins backlogin(haipumi_admins admin) {
		haipumi_admins backadmin=null;
		String sql="select * from haipumi_admins where aname=? and apwd=? and astatus!=2 ";
		List<Object>params=new ArrayList<Object>();
		String md5pwd=Encrypt.md5(admin.getApwd());
		params.add(admin.getAname());
		params.add(md5pwd);
		List list=null;
		try {
			list=db.finds(sql, params, haipumi_admins.class);
			if(!list.isEmpty()){
				backadmin=(haipumi_admins) list.get(0);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return backadmin;
	}
	@Override
	public List<haipumi_admins> findAllAdmins() {
		String sql="select * from haipumi_admins ";
		List<haipumi_admins> admins=null;
		try {
			admins=db.finds(sql, null, haipumi_admins.class);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return admins;
	}
	@Override
	public int countAllAdmins() {
		String sql="select count(*) from haipumi_admins ";
		int r=0;
		try {
			r=(int) db.findDouble(sql, null);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return r;
	}
	@Override
	public haipumi_admins insertAdmin(haipumi_admins admin) {
		String sql=null;
		int result=0;
		haipumi_admins backadmin=null;
		List<Object>params=new ArrayList<Object>();
		sql="insert into haipumi_admins (aname,apwd,astatus) values (?,?,default) ";
		String md5pwd=Encrypt.md5(admin.getApwd());
		params.add(admin.getAname());
		params.add(md5pwd);
		try {
			//result=db.doUpdate(sql, params);
			result=db.insertWithKey(sql, params);
			backadmin=new haipumi_admins();
			backadmin.setAid(Integer.toString(result));
			backadmin.setAname(admin.getAname());
			backadmin.setApwd(md5pwd);
			backadmin.setAstatus("0");
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return backadmin;
	}
	
	@Override
	public haipumi_admins findAdmin(int aid){
		String sql="select * from haipumi_admins where aid=? ";
		List<Object>params=new ArrayList<Object>();
		params.add(aid);
		haipumi_admins backadmin=null;
		List<haipumi_admins>backadmins=null;
		try {
			backadmins=db.finds(sql, params, haipumi_admins.class);
			backadmin=backadmins.get(0);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return backadmin;
	}
	
	@Override
	/**
	 * 修改成功，返回成功后的admin，不成功，返回传进来的admin
	 */
	public haipumi_admins editAdmin(haipumi_admins admin) {
		String sql=" update haipumi_admins set aname=? , apwd=? , astatus=? where aid=? ";
		List<Object>params=new ArrayList<Object>();
		haipumi_admins backadmins=findAdmin(Integer.parseInt(admin.getAid()));
		haipumi_admins backadmin=null;
		int result=0;
		String md5pwd=Encrypt.md5(admin.getApwd());
		String oldPwd=backadmins.getApwd().toString();
		String newPwd=admin.getApwd().toString();
		if(newPwd.equals(oldPwd)){
			//两次密码相同，密码不更改
			params.add(admin.getAname());
			params.add(admin.getApwd());
			params.add(admin.getAstatus());
			params.add(admin.getAid());
			try {
				result=db.doUpdate(sql, params);
				if(result>0){
					//修改成功
					backadmin=admin;
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else{
			//更改密码
			params.add(admin.getAname());
			params.add(md5pwd);
			params.add(admin.getAstatus());
			params.add(admin.getAid());
			try {
				result=db.doUpdate(sql, params);
				if(result>0){
					backadmin=admin;
					backadmin.setApwd(md5pwd);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return backadmin;
	}

}
